package com.suneee.enterprise.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.suneee.context.page.Page;
import com.suneee.context.page.Sort;
import com.suneee.context.page.Sort.Order;
import com.suneee.core.util.StringUtils;
import com.suneee.enterprise.bean.EnterpriseSearchBean;
import com.suneee.enterprise.dao.EnterpriseDao;
import com.suneee.enterprise.entity.Enterprise;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;

@Repository("enterpriseDao")
public class EnterpriseDaoImpl extends GenericDao<Enterprise, String>implements EnterpriseDao {

	/**
	 * 判断是否重复
	 * 
	 * @param name
	 * @param value
	 * @return
	 */
	private boolean exists(String name, String value) {
		List<Enterprise> list = this.selectWhere(name + " = ? ", value);
		return list != null && list.size() > 0;
	}

	/**
	 * 判断企业编码是否重复
	 */
	@Override
	public boolean existsCode(String name) {
		return this.exists("enterprise_code", name);
	}
	
	/**
	 * 判断企业邮箱是否重复
	 */
	@Override
	public boolean existsMail(String mail) {
		return this.exists("enterprise_mail", mail);
	}
	/**
	 * 判断企业简称是否唯一
	 */
	@Override
	public boolean existsShortName(String shortName) {
		return this.exists("short_name", shortName);
	}
	
	/**
	 * 判断企业简称是否唯一
	 */
	@Override
	public boolean existsName(String name) {
		return this.exists("name", name);
	}
	
	/**
	 * 查询品牌表Enterprise中符合条件的数据
	 */
	@Override
	public Page<Enterprise> find(EnterpriseSearchBean bean) {
		if (bean == null) {
			bean = new EnterpriseSearchBean();
		}
		WhereClauseBuilder builder = new WhereClauseBuilder();
		builder.contains("enterprise_code", bean.getEnterpriseCode());
		builder.contains("name", bean.getName());
		builder.equals("type", bean.getType());

		bean.setPageNumber(bean.getPageNumber() == 0 ? 1 : bean.getPageNumber());
		bean.setPageSize(bean.getPageSize() == 0 ? 10 : bean.getPageSize());
		bean.setSort((bean.getSort() == null || StringUtils.isEmpty(bean.getSort().getOrderByClause()))
				? new Sort("enterprise_code", Order.DESC) : bean.getSort());
		return selectWhere(builder.getSql(), bean, builder.getArgs());
	}

	/**
	 * 获取单条企业信息
	 * 
	 * @param enterpriseCode
	 * @return
	 */
	@Override
	public List<Enterprise> get(String enterpriseCode) {
		return this.selectWhere(" enterprise_code=?", enterpriseCode);
	}

	/**
	 * 删除企业信息根据企业编码
	 */
	@Override
	public void deleteByEnterpriseCode(String enterpriseCode) {
		this.deleteWhere(" enterprise_code=?", enterpriseCode);
	}

	private String updateEnterpriseSQL(Enterprise enterprise) {
		StringBuffer sb = new StringBuffer();
		sb.append("update t_enterprise set ");
		// sb.append(" enterprise_code='" + enterprise.getEnterpriseCode() + "'
		// ");
		sb.append(" short_name=? ");
		sb.append(" , name=? ");
		sb.append(" , logo=? ");
		sb.append(" , address=? ");
		sb.append(" , contact=? ");
		sb.append(" , telephone=? ");
		sb.append(" , enterprise_mail=? ");
		sb.append(" , remark=? ");
		// sb.append(" , enabled=" + enterprise.isEnabled());
		sb.append(" , type=? ");
		// sb.append(" and create_time=" + enterprise.getCreateTime());
		sb.append(" , last_update_time=? ");
		// sb.append(" and review_statue='" + enterprise.getReviewStatue() + "'
		// ");
		// sb.append(" and review_desctiption='" +
		// enterprise.getReviewDesctiption() + "' ");
		sb.append(" , business_license=? ");
		sb.append(" , legal_person=? ");
		sb.append(" , out_mall_code=? ");
		sb.append(" where enterprise_code=? ");
		

		return sb.toString();
	}

	/**
	 * 根据企业编码修改企业信息
	 */
	@Override
	public void updateEnterprise(Enterprise enterprise) {
		String sql = updateEnterpriseSQL(enterprise);
		Object[] arg = new Object[14];
		arg[0] = enterprise.getShortName();
		arg[1] = enterprise.getName();
		arg[2] = enterprise.getLogo();
		arg[3] = enterprise.getAddress();
		arg[4] = enterprise.getContact();
		arg[5] = enterprise.getTelephone();
		arg[6] = enterprise.getEnterpriseMail();
		arg[7] = enterprise.getRemark();
		arg[8] = enterprise.getType();
		arg[9] = enterprise.getLastUpdateTime();
		arg[10] = enterprise.getBusinessLicense();
		arg[11] = enterprise.getLegalPerson();
		arg[12] = enterprise.getOutMallCode();
		arg[13] = enterprise.getEnterpriseCode();
		this.executeUpdate(sql, arg);
	}

	/**
	 * 企业禁用
	 */
	@Override
	public void disabledEnterprise(String enterpriseCode) {
		Object[] arg = new Object[2];
		arg[0] = false;
		arg[1] = enterpriseCode;
		this.executeUpdate("update t_enterprise set enabled=? where enterprise_code=?", arg);
	}

	/**
	 * 企业激活
	 */
	@Override
	public void enabledEnterprise(String enterpriseCode) {
		Object[] arg = new Object[2];
		arg[0] = true;
		arg[1] = enterpriseCode;
		this.executeUpdate("update t_enterprise set enabled=? where enterprise_code=?", arg);

	}

	/**
	 * 企业审核(成功,未通过)
	 */
	@Override
	public void audit(String enterpriseCode, String type, String reviewDesctiption) {
		Object[] arg = new Object[3];
		arg[0] = type;
		arg[1] = reviewDesctiption;
		arg[2] = enterpriseCode;
		this.executeUpdate("update t_enterprise set review_statue=? ,review_desctiption=? where enterprise_code=?", arg);
	}

	@Override
	public List<Enterprise> selectEnterpriseQuantity(String enterpriseCode) {
		enterpriseCode = enterpriseCode+"%";
		List<Enterprise> list = this.executeQuery("select enterprise_code from t_enterprise where enterprise_code like ? ",enterpriseCode);
		return list;
	}

	

	@Override
	public Enterprise getByName(String name) {
		Enterprise enterprise = null;
		List<Enterprise> list = selectWhere(" name=? ", name);
		if(list != null && list.size() > 0)
			enterprise= list.get(0);
		return enterprise;
	}

	
}
